<template>
  <section class="headmenu-wrapper">
    <div class="menu-wrap knife_container clearfix">
      <!-- <a href="javascript:void 0;" class="menu-categories" @mouseover="thirdover" @mouseleave="thirdleave" ref="allCategoryRef">全部商品分类</a> -->
      <!-- <ul class="menu-list">
        <li class="menu-item">
          <router-link :to="{name:'BrandCenter'}" target="_blank" >品牌中心</router-link>
        </li>
        <li class="menu-item">
          <a href="javascript:void 0;">优刃专区</a>
        </li>
        <li class="menu-item">
          <a href="javascript:void 0;">大客户合作</a>
        </li>
        <li class="menu-item">
          <a href="javascript:void 0;">城市合伙人</a>
        </li>
        <li class="menu-item">
          <a href="javascript:void 0;">供应商合作</a>
        </li>
      </ul> -->
      <div class='site-category' v-if="$route.name === 'Index' || hasMouseOver">
        <ul class='site-category-list ' @mouseover="thirdover" @mouseleave="thirdleave" ref="categoryref">
          <div class="item_box" >
            <li v-for='(item, index) in menuList' :key='index' @mouseover='menuover(index, item.id)' class="category-item" :class="{'category-item-active': activeTab=== index}" >
              <router-link :to="{path:'/productlist', query:{firstId: secItem.pid, secId: secItem.id, type: 1, typeId: secItem.id}}"
              v-for='(secItem, secIndex) in item.typeList' :key='secIndex' class="secondrouter">
              {{secItem.alias}}
            </router-link>
            <i class="el-icon-arrow-right"></i>
            </li>
          </div>
          <!-- v-if="thirdmenu.typeList" -->
          <div v-if="thirdmenu.typeList" class='showThird' ref="thirdref">
            <div class="thirditem" v-for='(thitem, thindex) in thirdmenu.typeList' :key='thindex'>
              <h2>{{thitem.alias}}></h2>
              <div class="third-detail">
                <router-link :to="{path:'/productlist', query:{firstId: firstIdVal, secId: childItem.pid, thirdId: childItem.id, type: 2, typeId: childItem.id}}"
                v-for='(childItem, childIndex) in thitem.typeList' :key='childIndex' class="th_text">{{childItem.name}}</router-link>
              </div>
            </div>
          </div>
        </ul>
      </div>
    </div>
  </section>
</template>

<script>
import { getIndexMenulist } from '@/service/get-data'
export default {
  name: 'HeadMenu',
  data () {
    return {
      hasMouseOver: false,
      isHidden: false,
      menuList: null, // 菜单列表
      activeTab: -1,
      firstIdVal: '', // 1级类目的id
      leveloneid: '', // 三级类目点击时 获取一级的id
      thirdList: {}, // 三级目录列表
      thirdmenu: {} // 当前选中的三级详情
    }
  },
  methods: {
    // menu 二级菜单
    async getMenulist () {
      let res = await getIndexMenulist()
      if (res.data.resultCode && res.data.resultCode === 2000) {
        res = res.data
        this.menuList = res.data
      } else {
        this.$message({message: res.data.resultMsg})
      }
    },
    menuover (index, id) {
      this.activeTab = index
      let _obj = this.menuList.filter((item, index) => {
        return item.id === id
      })
      if (_obj.length > 0) {
        this.firstIdVal = _obj[0].typeList[0].pid // 一级类目的id 与二级的pid不一致
      }
      this.thirdList = _obj
    },
    thirdover (e) {
      // let allCategory = this.$refs['allCategoryRef']
      // allCategory.contains(e.target)
      let category = this.$refs['categoryref']
      let third = this.$refs['thirdref']
      if (category.contains(e.target) || (third && third.contains(e.target))) {
        this.hasMouseOver = true
        return
      }
      this.hasMouseOver = false
      this.activeTab = -1
      this.thirdList = {}
    },
    thirdleave (e) {
      this.hasMouseOver = false
      this.activeTab = -1
      this.thirdList = {}
    }
  },
  mounted () {
    this.getMenulist()
  },
  watch: {
    thirdList (val) {
      let list = val
      let _obj = {}
      if (list.length > 0) {
        list.map((item, index) => {
          _obj.leoneid = item.id
          _obj.typeList = item.typeList
        })
        this.thirdmenu = _obj
        this.leveloneid = _obj.leoneid
      } else {
        this.thirdmenu = {}
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  $font_ora:#FF7300;
  $font_yellow:#FFCC00;
.headmenu-wrapper{
  // height: 30px;
  // line-height: 30px;
  background-color: #fff;
}
.menu-wrap{
  position: relative;
  .menu-categories{
    float: left;
    width: 190px;
    height: 30px;
    background: #eee;
    text-indent: 20px;
    font-size: 11px;
    color: #999999;
  }
  .menu-list{
    overflow: hidden;
    padding-left: 10px;
  }
  .menu-item{
    float: left;
    padding-right: 20px;
    a{
      display: block;
    /* height: 38px; */
    /* line-height: 38px; */
    font-size: 14px;
    text-align: center;
    z-index: 100;
    color: #333333;
    }
  }

}
.site-category{
  width: 120px;
  .site-category-list{
    position: absolute;
    display: inline-block;
    width: 190px;
    height: 420px;
    background: rgba(47, 47, 47, 0.8);
    padding: 10px 0;
    z-index: 20;
    left: 0;
    top: 10px;
    box-sizing: border-box;
  }
}
.secondrouter{
  cursor:pointer;
}
  .category-item{
    position: relative;
    font-size: 12px;
    line-height: 17px;
    width: 100%;
    padding: 12px 0 12px 20px;
    box-sizing: border-box;
    color: #fff;
    a{
      position: relative;
      padding-right: 5px;
      color: #fff;
      display: inline-block;
    }
    i{
      position: absolute;
      top: 50%;
      right: 10px;
      transform: translateY(-50%);
    }
  }
  .category-item-active{
    background:rgba(199,199,199,0.7);
  }
.children-item{
  position: absolute;
  display: none;
  left: 100%;
  top: 0;
  z-index: 24;
  height: 488px;
  border: 1px solid #e0e0e0;
  border-left: 0;
  background: #fff;
  -webkit-box-shadow: 0 8px 16px rgba(0,0,0,0.18);
  box-shadow: 0 8px 16px rgba(0,0,0,0.18);
}
.arrow{
  float:right;
  color:#ffffff;
  line-height: 40px;
}
.category-item-active .children-item{
  display: block;
}
.showThird{
  font-family: PingFangSC-Medium, Microsoft YaHei;
  position: absolute;
  left: 190px;
  top: 0;
  z-index: 50;
  // width: 909px;
  width: 700px;
  height: 421px;
  background: #eee;
  padding: 12px 60px 25px 0;
  box-sizing: border-box;
}
.thirditem{
  position:relative;
  overflow: hidden;
  margin-top: 20px;
    h2{
      width:122px;
      float: left;
      text-align: left;
      color: #333;
      line-height: 15px;
      font-size: 12px;
      padding-right: 20px;
      text-align: right;
      box-sizing: border-box;
      line-height: 22px;
    }
    .third-detail{
      margin-left:122px;
      padding-bottom: 6px;
      border-bottom: 1px solid #E8E8E8;
    }
    a{
      cursor:pointer;
      display:inline-block;
      padding: 0 10px;
      line-height: 15px;
      font-size: 12px;
      margin-bottom: 14px;
      color: #666666;
      border-left: 1px solid #666666;
      &:hover{
        color: $font_yellow;
      }
    }
}
</style>
